'''
Created on 13/05/2009

@author: Edward
'''
from yarra.datastore.dst import DST
from yarra.datastore.identitymap import IdentityMap, IdentityMapException,\
    IdentityMapNotFoundException
from yarra.datastore.dso import DSO
import unittest


class IdentityMapTest(unittest.TestCase):

    def get_dst1(self):
        return DST(id=1, 
            subject='subject1', 
            predicate='predicate1',
            literal='literal1', 
            literal_type='literaltype')
    
    def get_dst2(self):
        return DST(id=2, 
            subject='subject1', 
            predicate='predicate1',
            literal='literal1', 
            literal_type='literaltype')
    
    def setUp(self):
        pass


    def tearDown(self):
        pass

    def test_get_dso_case_1(self):
        """ Tests getting a dso that is not there """
        identity_map = IdentityMap()
        try:
            identity_map.get_dso(1, 1)
            self.fail('A DSO shouldnt exist in the map yet')
        except IdentityMapNotFoundException:
            assert True   
                        
    def test_add_dso_case_1(self):
        """ Tests adding dsos """
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, version=1, dsts=set([dst1]))
        dst2 = self.get_dst2()
        dso2 = DSO(id=1, version=2, dsts=set([dst1, dst2]))
        identity_map = IdentityMap()
        identity_map.add_dso(dso1)
        identity_map.add_dso(dso2)
        copy_dso1 = identity_map.get_dso(dso1.id, dso1.version)
        copy_dso2 = identity_map.get_dso(dso2.id, dso2.version)
        if copy_dso1 is dso1 and copy_dso2 is dso2:
            assert True
        else:
            self.fail('Returned instance should be the same')
    
    def test_add_dso_case_2(self):
        """ Tests adding a dso without a id"""
        dst1 = self.get_dst1()
        dso1 = DSO(version=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        try:
            identity_map.add_dso(dso1)
            self.fail('Should not be able to add a dso without a id')
        except IdentityMapException:
            assert True        
        
    def test_add_dso_case_3(self):
        """ Tests adding a dso without a version number"""
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        try:
            identity_map.add_dso(dso1)
            self.fail('Should not be able to add a dso without a version number')
        except IdentityMapException:
            assert True  
            
    def test_add_dso_case_4(self):
        """ Tests adding a dso without a id or version number"""
        dst1 = self.get_dst1()
        dso1 = DSO(dsts=set([dst1]))
        identity_map = IdentityMap()
        try:
            identity_map.add_dso(dso1)
            self.fail('Should not be able to add a dso without a id or version number')
        except IdentityMapException:
            assert True                    
                
    def test_remove_dso_case_1(self):
        """ Tests removing a dso """
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, version=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        identity_map.add_dso(dso1)
        copy_dso1 = identity_map.get_dso(dso1.id, dso1.version)
        if copy_dso1 is dso1:
            identity_map.remove_dso(copy_dso1)
            try:
                identity_map.get_dso(dso1.id, dso1.version)
                self.fail('DSO should have been removed from identity map')
            except IdentityMapNotFoundException:
                assert True                
        else:
            self.fail('Returned instance should be the same')

    def test_remove_dso_case_2(self):
        """ Tests removing a dso that is not present in the Identity Map """
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, version=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        try:
            identity_map.remove_dso(dso1)
            self.fail('Should not be able to remove a dso that is not there')
        except IdentityMapException:
            assert True
                        
    def test_remove_dso_case_3(self):
        """ Tests removing a dso with no id"""
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, version=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        identity_map.add_dso(dso1)
        try:
            dso2 = DSO(version=1)
            identity_map.remove_dso(dso2)
            self.fail('Should not be able to remove a dso with no id')
        except IdentityMapException:
            assert True

    def test_remove_dso_case_4(self):
        """ Tests removing a dso with no version"""
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, version=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        identity_map.add_dso(dso1)
        try:
            dso2 = DSO(id=1)
            identity_map.remove_dso(dso2)
            self.fail('Should not be able to remove a dso with no version')
        except IdentityMapException:
            assert True  
            
    def test_remove_dso_case_5(self):
        """ Tests removing a dso with no id or version"""
        dst1 = self.get_dst1()
        dso1 = DSO(id=1, version=1, dsts=set([dst1]))
        identity_map = IdentityMap()
        identity_map.add_dso(dso1)
        try:
            dso2 = DSO()
            identity_map.remove_dso(dso2)
            self.fail('Should not be able to remove a dso with no id or version')
        except IdentityMapException:
            assert True
                     
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()